package negocio;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import beans.PlatoVO;

@Entity
@Table(name="platos")
public class Plato implements Serializable 
{
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id_plato", nullable=false)
	private int id_plato;
	
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="id_receta", referencedColumnName="id_receta")
	private Receta receta;
	
	@Column(name="precio",nullable=true)
	private float precio;
	
	@Column(name="nombre",nullable=true)
	private String nombre;

	@ManyToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="id_area", referencedColumnName="id_area")
	private Area area;
	
	@Column(name="comision_extra",nullable=true)
	private float comision_extra;
	
	@Column(name="rubro",nullable=true)
	private String Rubro;
	
	public Plato()
	{
		receta = new Receta();
		area = new Area();
	}
	
	public Plato(Receta receta, float precio, String nombre,
			float comision_extra, String rubro) {
		super();
		this.receta = receta;
		this.precio = precio;
		this.nombre = nombre;
		this.comision_extra = comision_extra;
		Rubro = rubro;
	}
	
	public Receta getReceta() {
		return receta;
	}
	public void setReceta(Receta receta) {
		this.receta = receta;
	}
	public float getPrecio() {
		return precio;
	}
	public void setPrecio(float precio) {
		this.precio = precio;
	}
	public String getNombre() {
		return nombre;
	}
	public void setNombre(String nombre) {
		this.nombre = nombre;
	}
	public float getComision_extra() {
		return comision_extra;
	}
	public void setComision_extra(float comision_extra) {
		this.comision_extra = comision_extra;
	}
	public String getTextRubro() {
		return Rubro;
	}
	public void setTextRubro(String textRubro) {
		Rubro = textRubro;
	}		
	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	@Override
	public String toString() {
		return "Plato [id_plato=" + id_plato + ", receta=" + receta.getId_receta()
				+ ", precio=" + precio + ", nombre=" + nombre + ", area="
				+ area.getNombre() + ", comision_extra=" + comision_extra + ", Rubro="
				+ Rubro + "]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((Rubro == null) ? 0 : Rubro.hashCode());
		result = prime * result + Float.floatToIntBits(comision_extra);
		result = prime * result + id_plato;
		result = prime * result + ((nombre == null) ? 0 : nombre.hashCode());
		result = prime * result + Float.floatToIntBits(precio);
		result = prime * result + ((receta == null) ? 0 : receta.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Plato other = (Plato) obj;
		if (Rubro == null) {
			if (other.Rubro != null)
				return false;
		} else if (!Rubro.equals(other.Rubro))
			return false;
		if (Float.floatToIntBits(comision_extra) != Float
				.floatToIntBits(other.comision_extra))
			return false;
		if (id_plato != other.id_plato)
			return false;
		if (nombre == null) {
			if (other.nombre != null)
				return false;
		} else if (!nombre.equals(other.nombre))
			return false;
		if (Float.floatToIntBits(precio) != Float.floatToIntBits(other.precio))
			return false;
		return true;
	}
	public void buildFromVO(PlatoVO platoVO)
	{
		Receta rec = new Receta();
		rec.buildFromVO(platoVO.getReceta());
		this.receta = rec;
		this.precio = platoVO.getPrecio();
		this.nombre = platoVO.getNombre();
		this.Rubro = platoVO.getRubro();
		this.comision_extra = platoVO.getComision_extra();
	}
	
	public PlatoVO getVO()
	{
		PlatoVO pltVO = new PlatoVO();
		pltVO.setArea(area.getNombre());
		pltVO.setComision_extra(getComision_extra());
		pltVO.setNombre(getNombre());
		pltVO.setPrecio(getPrecio());
		pltVO.setReceta(getReceta().getVO());
		pltVO.setRubro(getTextRubro());
		
		return pltVO;
	}
	public int getId_plato() {
		return id_plato;
	}
	public void setId_plato(int id_plato) {
		this.id_plato = id_plato;
	}
	
	public boolean isDisponible()
	{
		return area.isPlatoDisponible(this);
	}
	
	public void agregarComoPendiente()
	{
		area.agregarPlato(this);
	}
}
